<?php
class item_ribbons extends template_item
{
	var $default_name = "Add an Ribbon";
	var $default_description = "Add an effect to your name with an addition to others.";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['ribbon'] = '';
			$this->item_config['multiple'] = 0;
		}
	}
	
	function admin_form()
	{
		$ribbons = array();
		if($this->item['item_id'] == 0)
		{
			$ribbons[0] = array('','Select A Ribbon');
		}
		$list = $this->ipsclass->market_class->get_file_list($this->ipsclass->market_class->image_server_path."/ribbons/");
		foreach($list as $file)
		{
			if(preg_match("/(.jpg|.gif|.png)/",$file))
			{
				if($file != '.' || $file  != '..')
				{
					$ribbons[] = array($file,$file);
				}
			}
		}
		if(count($ribbons) == 0)
		{
			$this->ipsclass->admin->error("You need some ribbons first. Please upload some to: /images/market/ribbons/");
		}
		if($this->item['item_id'] == 0)
		{
			$image = $ribbons[mt_rand(0,count($ribbons)-1)][0];
			$ribbons[0] = array($image,'Select A Ribbon');
			$image = $this->ipsclass->market_class->image_url_path."/ribbons/".$image;
		}
		else
		{
			$image = $this->ipsclass->market_class->image_url_path."/ribbons/".$this->item_config['ribbon'];
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which ribbon to add?</b>",
															$this->ipsclass->adskin->form_dropdown("item[".$this->item['item_id']."][ribbon]",$ribbons,$this->item_config['ribbon'],"onChange='form_change_ribbon(this.value,".$this->item['item_id'].")'") . "<img src='".$image."' name='ribbon_".$this->item['item_id']."_preview' />",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Number of times that ribbons can be added</b><br />Input 0 to add it unllimited times.",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][multiple]",$this->item_config['multiple']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['ribbon'] = $input['ribbon'];
		$save['multiple'] = $input['multiple'];
		
		$item_info['item_config'] = serialize($save);
		
		if($this->ipsclass->DB->field_exists("topic_ribbons", "members"))
		{
			$this->ipsclass->DB->sql_change_field("members","topic_ribbons","topic_ribbons","TEXT NOT NULL","''");
		}
		else
		{
			$this->ipsclass->DB->sql_add_field("members","topic_ribbons","TEXT NOT NULL","''");
		}
	}
	
	function admin_remove()
	{
		if(count($this->ipsclass->market_class->item_by_folder[$this->item['filename']]) == 1)
		{
			$this->ipsclass->DB->sql_drop_field("members","topic_ribbons");
		}
	}
	
	function admin_view_file_edits()
	{
		$edits = "";
		
		$edits .= $this->ipsclass->adskin->start_table("Files to edit");
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"/index.php<br />/sources/action_public/profile.php<br />/sources/sql/mysql_queries.php<br />/sources/ipsclass.php",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();

		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("/index.php");
		$edits .= $this->ipsclass->adskin->add_td_basic('About line 64', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>require_once KERNEL_PATH . \"class_converge.php\";\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>require_once ROOT_PATH   . \"sources/market/ribbons.php\";\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It will look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>require_once KERNEL_PATH . \"class_converge.php\";\nrequire_once ROOT_PATH   . \"sources/market/ribbons.php\";\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("/sources/action_public/profile.php");
		$edits .= $this->ipsclass->adskin->add_td_basic('About line 3159', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>		//-----------------------------------------\n		// Online location\n		//-----------------------------------------\n		\n		\$member = \$this->personal_portal_get_user_location( \$member );\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Under the function",
																"view_profile()",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add before",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>		\$member['topic_ribbons'] = member_ribbons(\$member);\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It will look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>		\$member['topic_ribbons'] = member_ribbons(\$member);\n		//-----------------------------------------\n		// Online location\n		//-----------------------------------------\n		\n		\$member = \$this->personal_portal_get_user_location( \$member );\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_basic('About line 3948', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>    	\$info['members_display_name'] = \$member['members_display_name'];\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Under the function",
																"view_profile()",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>    	\$info['topic_ribbons'] = member_ribbons(\$member);\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It will look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>    	\$info['members_display_name'] = \$member['members_display_name'];\n    	\$info['topic_ribbons'] = member_ribbons(\$member);\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("/sources/sql/mysql_queries.php");
		$edits .= $this->ipsclass->adskin->add_td_basic('About line 167', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>				me.msnname,me.aim_name,me.icq_number,me.signature, me.website,me.yahoo,me.location, me.avatar_location, me.avatar_type, me.avatar_size, m.members_display_name</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Under the function",
																"topics_get_posts( \$a )",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>,m.topic_ribbons</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It will look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>				me.msnname,me.aim_name,me.icq_number,me.signature, me.website,me.yahoo,me.location, me.avatar_location, me.avatar_type, me.avatar_size, m.members_display_name,m.topic_ribbons</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_basic('About line 182', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>				me.msnname,me.aim_name,me.icq_number,me.signature, me.website,me.yahoo,me.location, me.avatar_location, me.avatar_type, me.avatar_size, m.members_display_name,</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Under the functions",
																"topics_get_posts_with_join( \$a )",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>m.topic_ribbons,</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>				me.msnname,me.aim_name,me.icq_number,me.signature, me.website,me.yahoo,me.location, me.avatar_location, me.avatar_type, me.avatar_size, m.members_display_name,m.topic_ribbons,</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("/sources/ipsclass.php");
		$edits .= $this->ipsclass->adskin->add_td_basic('About line 6058', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>		//-----------------------------------------\n		// Photo and such\n		//-----------------------------------------\n		\n		\$member = \$this->member_set_information( \$member );\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Under the function",
																"parse_member( \$member=array(), \$custom_fields=1, \$skin_file='skin_topic' )",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add before",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>		\$member['topic_ribbons'] = member_ribbons(\$member);\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>		\$member['topic_ribbons'] = member_ribbons(\$member);\n		//-----------------------------------------\n		// Photo and such\n		//-----------------------------------------\n		\n		\$member = \$this->member_set_information( \$member );\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$edits .= $this->ipsclass->adskin->start_table("Templates to edit");
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																			"Profile View Skin<br />- personal_portal_main<br />- show_profile<br />Topic View Skin<br />- RenderRow",
																	)	);
		$edits .= $this->ipsclass->adskin->end_table();

		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("Topic View Skin");
		$edits .= $this->ipsclass->adskin->add_td_basic('RenderRow', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"How to get there",
																"Go click the LOOK & FEEL tab on top or <a href='".$this->ipsclass->base_url."&amp;section=lookandfeel' target='_blank'>here</a> open a new window.<br />Click Skin Manager on the left.<br />On each skin, clikc the options icon and click Edit Template HTML.<br />Click Topic View, then click RenderRow on the right side.",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$author['member_rank_img']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><br />{\$author['topic_ribbons']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$author['member_rank_img']}<br />{\$author['topic_ribbon']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("Profile View Skin");
		$edits .= $this->ipsclass->adskin->add_td_basic('personal_portal_main', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"How to get there",
																"Click Profile View on the bottom left, then click personal_portal_main on the bottom right side.",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find on the top",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$member['_member_rank_img']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><br />{\$member['topic_ribbons']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$member['_member_rank_img']}<br />{\$member['topic_ribbons']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_basic('show_profile', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"How to get there",
																"Click show_profile on the bottom right side.",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find on the top",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$info['member_rank_img']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><br />{\$info['topic_ribbons']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$info['member_rank_img']}<br />{\$info['topic_ribbons']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		return $edits;
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$ribbons = $this->ipsclass->member['topic_ribbons'] ? unserialize($this->ipsclass->member['topic_ribbons']) : array();
		$ribbons = is_array($ribbons) ? $ribbons : array();
		
		if(!isset($ribbons[$this->item['item_id']]) || !is_array($ribbons[$this->item['item_id']]))
		{
			$ribbons[$this->item['item_id']] = array();
		}
		if($this->item_config['multiple'] && count($ribbons[$this->item['item_id']]) + count($inventory_ids) > $this->item_config['multiple'])
		{
			$this->ipsclass->Error(array('MSG'=>'too_many_ribbons'));
		}
		foreach($inventory_ids as $inv_id)
		{
			$this->_write_log($inv_id, "Added ribbon <img src='".$this->ipsclass->market_class->image_url_path."/ribbons/".$this->item_config['ribbon']."' alt=''>");
			$ribbons[$this->item['item_id']][] = $this->item_config['ribbon'];
		}
		
		$this->ipsclass->DB->do_update('members',array(
															'topic_ribbons' => serialize($ribbons),
					),"id=".$this->ipsclass->member['id']);
		
		$this->delete_items($inventory_ids);
		
		$this->ipsclass->market_class->redirect('ribbons_added');
	}
}
?>